package com.ezmobo.core.common;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class SharedTimer {
	private SharedTimer() {

	}

	private static final ScheduledThreadPoolExecutor schedule = new ScheduledThreadPoolExecutor(4);

	/**
	 * 指定一个任务在多少时间后执行,执行一次
	 * 
	 * @param task
	 * @param delay
	 */
	public static synchronized void schedule(Runnable task, long delay) {
		if (delay < 0) {
			delay = 0;
		}
		schedule.schedule(task, delay, TimeUnit.MILLISECONDS);
	}

	/**
	 * 指定一个任务在多少时间后执行,重复执行
	 * 
	 * @param task
	 * @param delay
	 * @param period
	 */
	public static void schedule(Runnable task, long delay, long period) {
		if (delay < 0) {
			delay = 0;
		}
		if (period > 0) {
			schedule.scheduleWithFixedDelay(task, delay, period, TimeUnit.MILLISECONDS);
		} else {
			schedule.schedule(task, delay, TimeUnit.MILLISECONDS);
		}
	}
}
